<?php 
namespace app\Index\controller;
use think\Controller;
use think\Request;
use app\index\model\User;
class Workerc extends Base
{
    public function modify_pass()
    {
        if(Request::instance()->isPost()){
            $worker = input("post.worker");
            $pass = md5(input("post.password"));// 原密码，加密字符串
            $newpass = input("post.newpass");
            $repass = input("post.repass");

            $user = new User();
            $userinfo = $user -> get_user_info($worker);
            $upass = $userinfo['upass'];// 原密码，加密字符串

            if($pass != $upass){
                return json(array("status"=>0, "message"=>"原密码不正确，请查证后重试！"));
            }

            if($newpass != $repass){
                return json(array("status"=>0, "message"=>"新密码不一致，请重试！"));
            }

            
            $data = $user -> update_user("modify_pass", $worker, md5($newpass));
            if($data){
                return json(array("status"=>1, "message"=>"修改成功，请牢记您的新密码！"));
            }else{
                return json(array("status"=>0, "message"=>"修改失败，请联系系统管理员！"));
            }
        }
        return json(array("status"=>0, "message"=>"非法操作！","data"=>[]));    
    }


    public function worker_list($key=null)
    {
        if(Request::instance()->isAjax()){
            // $key 由ajax传入，也可用通过input('get.key.uid')来取得，使用input获取时，必须保证key的名称与ajax提交的名称一致
            $uid = is_array($key) ? isset($key['uid']) ? $key['uid'] : null : input("post.uid");
            $page = input("get.page") ? input("get.page")  : 1;//页码
            $limit = input("get.limit") ? input("get.limit") : 5;//每页条数
            $user = new User();
            $data = $user -> get_workers($uid, $page, $limit);
            // layui数据表格要求返回数据code必须为0
            return json(array("status"=>1, "code"=>0, "count"=>$data['count'], "message"=>"查询成功！", "data"=>$data["data"]));
        }
        return json(array("status"=>0, "code"=>0, "message"=>"非法查询！","data"=>[]));    
    }

    public function worker_able($worker, $option)
    {
        if(Request::instance()->isPost()){
            $worker = $worker;
            $status = 0;
            if($option == "disable"){$status = 1;}
            $user = new User();
            $data = $user -> update_user("worker_able", $worker, $status);// 返回受影响的行数
            if($data >= 0){
                return json(array("status"=>1, "message"=>"操作成功！", "data"=>$data));
            }else{
                return json(array("status"=>0, "message"=>"操作失败！", "data"=>[]));
            }
        }
        return json(array("status"=>0, "message"=>"非法操作！","data"=>[])); 
    }

    public function worker_repass($worker, $option)
    {
        if(Request::instance()->isPost()){
            if($option != "repass"){
                return json(array("status"=>0, "message"=>"非法操作！", "data"=>[]));
            }
            $user = new User();
            $data = $user -> update_user("worker_repass", $worker, md5($worker));
            if($data >= 0){
                return json(array("status"=>1, "message"=>"操作成功！", "data"=>$data));
            }else{
                return json(array("status"=>0, "message"=>"操作失败！", "data"=>[]));
            }
        }
        return json(array("status"=>0, "message"=>"非法操作！","data"=>[])); 
    }

    public function worker_create($worker, $nik, $sex, $identity, $birthday, $role)
    {
        if(Request::instance()->isPost()){
            $user = new User();
            $user_exits = $user -> get_user_info($worker);
            if(!$user_exits){
                $data = $user -> create_user(
                    $worker,
                    md5($worker),
                    $nik,
                    $sex,
                    $identity,
                    $birthday,
                    $role
                );
                if($data >= 0){
                    return json(array("status"=>1, "message"=>"操作成功！", "data"=>$data));
                }else{
                    return json(array("status"=>0, "message"=>"操作失败！", "data"=>[]));
                }                
            }
            return json(array("status"=>0, "message"=>"用户名已存在", "data"=>[]));
        }
        return json(array("status"=>0, "message"=>"非法操作！","data"=>[])); 
    }
}